import {checkLogin} from "./api/user";

export const imLoginPassword = function imLoginPassword(uid) {
    uni.WebIM.conn.open({
        appKey: '1148210630175399#demo',
        pwd: "admin@123",
        user: uid,
        success: (res) => {
            const {
                access_token
            } = res
            uni.setStorageSync('imToken', access_token)
        }

    })
}

// 环信token登录
export const imLogin = function imLogin() {
    let uid = uni.getStorageSync('uid')
    if (uid) {
        uni.WebIM.conn.open({
            appKey: "1148210630175399#demo",
            accessToken: uni.getStorageSync('imToken'),
            user: uni.getStorageSync('uid'),
            success: (res) => {
                const {
                    access_token
                } = res
                uni.setStorageSync('imToken', access_token)
            }
        });
    }
}


/**
 * 校验登录 确定登录信息
 */
export const loginCheck = function loginCheck() {
    const params = {
        uid: uni.getStorageSync('uid')
    }
    checkLogin(params).then(res => {
    }).catch(e => {
        console.log(e)
        if (e.status == 0 && e.msg == '该账号已在其他地方登录') {
            uni.removeStorage('uid')
            uni.removeStorage('token')
            uni.reLaunch({
                url: '/subpages/pages/login/login'
            })
        }
    })
}
